#include "keyboard.h"
#include "log.h"
#include "settings.h"

void keyboard::update()
{
    newKeys.clear();
    SDL_Event event;

    while (SDL_PollEvent(&event))
    {
        if (event.key.repeat)
            return;

        switch (event.type)
        {
            case SDL_KEYUP:
            {
                if (event.key.keysym.scancode == getInt("KEY_UP"))
                    newKeys.insert(std::pair<std::string, keyState>("KEY_UP", KEY_UP));
                if (event.key.keysym.scancode == getInt("KEY_LEFT"))
                    newKeys.insert(std::pair<std::string, keyState>("KEY_LEFT", KEY_UP));
                if (event.key.keysym.scancode == getInt("KEY_DOWN"))
                    newKeys.insert(std::pair<std::string, keyState>("KEY_DOWN", KEY_UP));
                if (event.key.keysym.scancode == getInt("KEY_RIGHT"))
                    newKeys.insert(std::pair<std::string, keyState>("KEY_RIGHT", KEY_UP));
                if (event.key.keysym.scancode == getInt("KEY_FULLSCREEN"))
                    newKeys.insert(std::pair<std::string, keyState>("KEY_FULLSCREEN", KEY_UP));
                if (event.key.keysym.scancode == SDL_SCANCODE_ESCAPE)
                    newKeys.insert(std::pair<std::string, keyState>("KEY_ESCAPE", KEY_UP));
                if (event.key.keysym.scancode == getInt("KEY_PAUSE"))
                    newKeys.insert(std::pair<std::string, keyState>("KEY_PAUSE", KEY_UP));
                if (event.key.keysym.scancode == getInt("KEY_PAUSE"))
                    newKeys.insert(std::pair<std::string, keyState>("KEY_PAUSE", KEY_UP));
                if (event.key.keysym.scancode == getInt("KEY_SLOW"))
                    newKeys.insert(std::pair<std::string, keyState>("KEY_SLOW", KEY_UP));
                if (event.key.keysym.scancode == getInt("KEY_SHOOT"))
                    newKeys.insert(std::pair<std::string, keyState>("KEY_SHOOT", KEY_UP));

                log(LOG_DEBUG, "Key up: %s (%d)\n", SDL_GetKeyName(event.key.keysym.sym), event.key.keysym.scancode);

                return;
            }

            case SDL_KEYDOWN:
            {
                if (event.key.keysym.scancode == getInt("KEY_UP"))
                    newKeys.insert(std::pair<std::string, keyState>("KEY_UP", KEY_DOWN));
                if (event.key.keysym.scancode == getInt("KEY_LEFT"))
                    newKeys.insert(std::pair<std::string, keyState>("KEY_LEFT", KEY_DOWN));
                if (event.key.keysym.scancode == getInt("KEY_DOWN"))
                    newKeys.insert(std::pair<std::string, keyState>("KEY_DOWN", KEY_DOWN));
                if (event.key.keysym.scancode == getInt("KEY_RIGHT"))
                    newKeys.insert(std::pair<std::string, keyState>("KEY_RIGHT", KEY_DOWN));
                if (event.key.keysym.scancode == getInt("KEY_FULLSCREEN"))
                    newKeys.insert(std::pair<std::string, keyState>("KEY_FULLSCREEN", KEY_DOWN));
                if (event.key.keysym.scancode == SDL_SCANCODE_ESCAPE)
                    newKeys.insert(std::pair<std::string, keyState>("KEY_ESCAPE", KEY_DOWN));
                if (event.key.keysym.scancode == getInt("KEY_SLOW"))
                    newKeys.insert(std::pair<std::string, keyState>("KEY_SLOW", KEY_DOWN));
                if (event.key.keysym.scancode == getInt("KEY_SHOOT"))
                    newKeys.insert(std::pair<std::string, keyState>("KEY_SHOOT", KEY_DOWN));

                log(LOG_DEBUG, "Key down: %s (%d)\n", SDL_GetKeyName(event.key.keysym.sym), event.key.keysym.scancode);

                return;
            }
        }
    }
}

bool keyboard::keyDown(std::string key)
{
    if ((exists(key)) && (newKeys[key] == KEY_DOWN))
        return true;

    return false;
}

bool keyboard::keyUp(std::string key)
{
    if ((exists(key)) && (newKeys[key] == KEY_UP))
        return true;

    return false;
}

bool keyboard::exists(std::string key)
{
   if (newKeys.count(key) == 0)
        return false;

    return true;
}
